import { piniaPersistConfig } from "@/config";
import type { LoginResponse } from "@/typings";
import { defineStore } from "pinia";
import { ref } from "vue";

const key: string = "useUserStore";

export const useUserStore = defineStore(
  key,
  () => {
    /**
     * @description 用户登录令牌
     */
    const token = ref<string>("");

    /**
     * @description 用户登录信息
     */
    const userInfo = ref<LoginResponse>({} as LoginResponse);

    /**
     * @description 清除缓存
     */
    function clearCacheHandler() {
      token.value = "";
      userInfo.value = {} as LoginResponse;
    }

    return {
      token,
      userInfo,
      clearCacheHandler
    };
  },
  {
    persist: piniaPersistConfig(key, sessionStorage)
  }
);
